package Dao;

import domain.news;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import uilts.C3P0utils;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class NewsDao {
    C3P0utils c3P0utils=new C3P0utils();
    /*
    * 查询出新闻标题
    * */

    public List<news> findAll(String sql){
        QueryRunner queryRunner = new QueryRunner(c3P0utils.getDataSource());
        try {
            List<news> query = queryRunner.query(sql, new BeanListHandler<news>(news.class));
            return  query;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }
/*新增*/
    public boolean addnews(String sql, String title, String body) {
        QueryRunner queryRunner = new QueryRunner(c3P0utils.getDataSource());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            int update = queryRunner.update(sql, title, body, simpleDateFormat.format(new Date()));

            if(update>0){
                return  true;
            }else{
                return  false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return  false;

    }

    public int update(String sql, List list) {
        return c3P0utils.executeUpdate(sql, list.toArray());
    }


    /*修改*/



}
